﻿# -*- coding: utf-8-*-
"""
     ┏┓   ┏┓
    ┏┛┻━━━┛┻┓
    ┃   ☃       ┃
    ┃  ┳┛┗┳    ┃
    ┃   ┻        ┃
    ┗━┓    ┏━┛
      ┃     ┗━━━┓
      ┃  神兽保佑    ┣┓
      ┃　永无BUG！         ┏┛
      ┗┓┓┏━┳┓┏┛
       ┃┫┫  ┃┫┫
       ┗┻┛  ┗┻┛
"""
import os
from configparser import ConfigParser
basedir = os.path.abspath(os.path.dirname(__file__))

def __init__():
    global cfg
    cfg = ConfigParser()
    configfile = os.path.join(os.path.dirname(__file__), 'config.ini')
    cfg.read(configfile)
    cfg.set('base', 'app_path', os.path.dirname(__file__))

__init__()

print(cfg.get('redis', 'host'))
DB_HOST = cfg.get('postgresql', 'host')
DB_PORT = cfg.get('postgresql', 'port')
DB_USERNAME = cfg.get('postgresql', 'username')
DB_PASSWORD = cfg.get('postgresql', 'password')
DB_DATABASE = cfg.get('postgresql', 'database')
# redis
REDIS_HOST = cfg.get('redis', 'host')
REDIS_PORT =cfg.get('redis', 'port')
REDIS_DB = cfg.get('redis', 'db')
REDIS_PASSWORD = cfg.get('redis', 'password')

"""
接口调用token密钥配置
api auth token secret
"""
AUTH_TOKEN_SECRET = cfg.get('auth_token', 'auth_token_secret')
AUTH_REFRESH_TOKEN_SECRET =cfg.get('auth_token', 'auth_refresh_token_secret')




"""基础配置"""


# create SECRET_KEY can by os.urandom(24)
class Config:
	CSRF_ENABLED = True
	SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard to guess string'
	SQLALCHEMY_COMMIT_ON_TEARDOWN = True
	SQLALCHEMY_RECORD_QUERIES = True
	SQLALCHEMY_TRACK_MODIFICATIONS = True
	SQLALCHEMY_ECHO = True
	SQLALCHEMY_NATIVE_UNICODE = False  # 默认编码开关
	SQLALCHEMY_POOL_SIZE = 10  # 默认是5
	SQLALCHEMY_POOL_TIMEOUT = 12  # 指定数据库连接池的超时时间。默认是 10
	SQLALCHEMY_POOL_RECYCLE = 5  # 自动回收连接的秒数
	EXPIRATION = 1200

	try:
		SQLALCHEMY_DATABASE_URI = 'postgres://' + DB_USERNAME + ':' + DB_PASSWORD + '@' + DB_HOST + ':' + DB_PORT + '/' + DB_DATABASE
	except Exception as e:
		print(e)
		raise
	if REDIS_PASSWORD:
		#"redis://:password@hostname:port/db_number'"
		#"'BROKER_URL = 'redis://localhost:6379/0'"
		CELERY_BROKER_URL = CELERY_RESULT_BACKEND = 'redis://:{}@{}:{}/{}'.format(REDIS_PASSWORD, REDIS_HOST, REDIS_PORT, REDIS_DB)
	else:
		CELERY_BROKER_URL = CELERY_RESULT_BACKEND = 'redis://{}:{}/{}'.format(REDIS_HOST, REDIS_PORT, REDIS_DB)
	CELERY_TASK_SERIALIZER = 'json'

	@staticmethod
	def init_app(app):
		pass

# DB_URI = 'postgresql+psycopg2://{}:{}@{}:{}/{}'.format(USERNAME, PASSWORD, HOSTNAME, PORT, DATABASE)

# 实际生产环境, 敏感信息从环境变量读取

class DevelopmentConfig(Config):
	@staticmethod
	def init_app(app):
		import logging
		from logging.handlers import SMTPHandler
		log_file = '/'.join([basedir, "nds_coalition_video.log"])
		log_file_error = '/'.join([basedir, "nds_coalition_video_error.log"])
		fmt = '%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
		format_str = logging.Formatter(fmt)  # 设置日志格式
		sh = logging.StreamHandler()  # 往屏幕上输出
		sh.setFormatter(format_str)  # 设置屏幕上显示的格式
		app.logger.addHandler(sh)


class ProductionConfig(Config):
	@staticmethod
	def init_app(app):

		import logging
		from logging.handlers import SMTPHandler
		log_file = '/'.join([basedir, "nds_coalition_video.log"])
		log_file_error = '/'.join([basedir, "nds_coalition_video_error.log"])
		fmt = '%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
		format_str = logging.Formatter(fmt)  # 设置日志格式
		sh = logging.StreamHandler()  # 往屏幕上输出
		sh.setFormatter(format_str)  # 设置屏幕上显示的格式

		file_handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=1024 * 1024 * 10, backupCount=10)
		file_handler.setFormatter(format_str)  # 设置文件里写入的格式

		file_handler_error = logging.handlers.RotatingFileHandler(log_file_error, maxBytes=1024 * 1024 * 10, backupCount=10)
		file_handler_error.setFormatter(format_str)
		file_handler_error.setLevel('ERROR')

		app.logger.addHandler(sh)
		app.logger.addHandler(file_handler)
		app.logger.addHandler(file_handler_error)


config = {
	'development': DevelopmentConfig,
	'production': ProductionConfig,
	'default': DevelopmentConfig
}




"""分页每页显示数量"""
note_per_page = 10
user_per_page = 10
note_per_body_page = 20

WX_APPID = "wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
WX_SECRET = "c0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"


ACCESS_KEY_ID = "xxxxx"
ACCESS_KEY_SECRET = "xxxxxxxxxxxxxxx"

DATA_PATH = "/home/zhang/workspace/data"
NGINX_HOST = "http://192.168.1.164:9000"

ROLE_TYPE = ["admin", "expert", "normal"]

UserInit = {
	"u1": {
		"mobile": "13312345678",
		"passwd": "12345678",
		"username": "admin",
		"nickname": "admin",
		"email": "13312345678@example.com",
		"role_type": "admin"
	},
	"u2": {
		"mobile": "13412345678",
		"passwd": "12345678",
		"username": "lilaoshi",
		"nickname": "laoshi",
		"email": "13412345678@example.com",
		"role_type": "expert"
	},
	"u3": {
		"mobile": "13512345678",
		"passwd": "12345678",
		"username": "chenxiaoming",
		"nickname": "xiaoming",
		"email": "13512345678@example.com",
		"role_type": "normal"
	}
}
